<html>
<?php
/**********************************INCLUDE*********************************** *
* **************************************************************************** */
include('../Lib/php/PlayerDef.php');
include('../Lib/php/GlyphDef.php');
//include('../Lib/php/SuperHTMLDef.php');
include('../Lib/php/DBMgrDef.php');
include('../Lib/php/HTMLMgrDef.php');
include('../Lib/php/GameMgrDef.php');

/*
* TODO: Add support for both HTMLMgrDef.php and GameMgrDef.php!
* 	   Once we've implemented HTMLMgrDef.php, we can remove SuperHTMLDef.php.
*/
?>

<?php
/*********************************CONSTANT*********************************** *
* **************************************************************************** */
define("HAND_SIZE", 6);
define("MAX_HEALTH", 100);
define("MAIN_LOGO", "http://localhost/Glyph/Game/logos/glyphlogo.png");
define("INVOKE_BTN", "http://localhost/Glyph/Game/menu/invoke.jpg");
define("CREATE_INK_BTN", "http://localhost/Glyph/Game/menu/ink.jpg");

/*
* TODO: Add any URLs, phpAdmin account names/passwords, database names, and table
* 	   names we utilize within this script as constants here.
*/
?>

<?php 
session_start();
error_reporting(E_ALL ^ E_NOTICE); 
?>

<?php
/************************************MAIN************************************ *
* Script Name: battle.php
* Author: cf.Studios
* Description: This is the main 'game' script of our game. Everything from
* playing cards, displaying hands, and any game mechanics are handled here.
* (Excluding combining cards, that is handled in the combine.php script.)
***************************************************************************** */
/* Creates an object to hold all of the HTML for our page. Also creates
 * a new entity to store what is happening in the game.
 */$HTML = new HTMLMgrDef("cf.Studios", "Glyph 0.1");
   $HTML->createEntity("gameReport");
   print "creating HTML<br>";

/* The main decision structure for the script. This determines what the GameMgr
 * should do according to what button the user pressed. In the future the  
 * 'start' action will be sent from the main.php script.
 */if (!isset($_REQUEST["action"]) || $_REQUEST["action"] == "start")
   {   
	/* Creates an object to hold all of the data for the game.
     */$GAME = new GameMgrDef("Player 1", "Player 2", MAX_HEALTH);
	 
    /* Starts the game.
     */$HTML->addHTML("gameReport", $GAME->startGame(HAND_SIZE));
		print "starting game<br>";
   }
   else if ($_REQUEST["action"] == "invoke")
   {
	   $GAME = $_SESSION["GAME"];
	   print "loading GAME<br>";
       $HTML->addHTML("gameReport", $GAME->playCard($_REQUEST["playedCard"]));
	   print "invoking<br>";
       
       /*TODO: End turn and start the next player's turn.*/
       //$HTML->addHTML("gameReport", $GAME->endTurn()); 
   }

   $_SESSION["GAME"] = $GAME;
   print "storing GAME<br>";
   
/* If we want to display what phase it is (or any additional info not stored
 * in PlayerDef), we need to send the entire GAME object. Right now just 
 * sending the players should be fine...
 */buildPage($GAME->getPlayers(), $HTML);
   print "building page<br>";
    
   print "printing page<br>";
/* Prints out the page we built.
 */print $HTML->getPage();

/*TODO: Add HTMLMgrDef and GameMgrDef functionality.*/
//    $report .= determineTurn($players);
//
//    $report .= applyEffects($players);
//    
//    foreach ($players as $player)
//    {
//        if ($player->isTurn())
//        {
//            $player->drawCards(HAND_SIZE);
//        }
//    }
//
// /* Stores the PlayerDef obj. array in a session variable.
//  */$_SESSION["players"] = $players;
//
//    $gamePage = buildPage($players, $report);
//
//    echo $gamePage;
//}
//else if ($_REQUEST["page"] == "ink")
//{
//    $players = $_SESSION["players"];
//
//    $report = addInk($players, MAX_HEALTH);
//
// /* Stores the PlayerDef obj. array in a session variable.
//  */$_SESSION["players"] = $players;
//
//    $gamePage = buildPage($players, $report);
//
//    echo $gamePage;
//}
//else if ($_REQUEST["page"] == "combBack")
//{
//    $players = $_SESSION["players"];
//
//    foreach ($players as $player)
//    {
//        if ($player->isTurn())
//        {
//         /* Places both removed cards back into the user's hand.
//          */$player->addHand($_SESSION["card1"]);
//            $player->addHand($_SESSION["card2"]);
//            $report = "It's now ".$player->getName()."'s turn!";
//        }
//    }
//
//    $_SESSION["players"] = $players;
//
// /* Builds the battle game page.
//  */$gamePage = buildPage($players, $report);
//
//    echo $gamePage;
//}
//else if ($_REQUEST["page"] == "combined")
//{
//    $players = $_SESSION["players"];
//
//    foreach ($players as $player)
//    {
//        if ($player->isTurn())
//        {
//            $report = $player->getName()." creates a Glyph of ".
//                      $_REQUEST["newCard"]."!\n";
//
//            $report .= $player->getName()." loses 1 ink!";
//        }
//    }
//    
// /* Builds the battle game page.
//  */$gamePage = buildPage($players, $report);
//
//    echo $gamePage;
//}
?>
    
<?php
/*TODO: Move to GameMgrDef!*/
/**********************************FUNCTION********************************** *
* Function Name: determineTurn
* Author: Nicholas Malacarne
* Parameters: $players0 - An array of PlayerDef objects.
* Description: Used to determine which player's turn it is.
***************************************************************************** */
//function determineTurn($players0)
//{
// /* Are either of the players DAZED?
//  */else if ($players0[0]->isDazed() || $players0[1]->isDazed())
//    {
//        foreach ($players0 as $player)
//        {
//            if ($player->isDazed())
//            {
//                $player->setTurn(true);
//                $HTML .= $player->getName()." skips his/her next turn!\n";
//                /*
//                $HTML->addText($player->getName()." skips his/her next turn!");
//                $HTML->newLine();*/
//                $report = $player->assignEffects();
//                $player->decayStatus();
//                $player->setTurn(false);
//
//                if ($report == null)
//                {
//                    break;
//                }
//                else
//                {
//                    $HTML .= $report;
//                    /*
//                    $HTML->addText($report);*/
//                }
//            }
//        }
//    }
//    else
//    {
//        foreach ($players0 as $player)
//        {
//            if ($player->isTurn())
//            {
//                $player->setTurn(false);
//            }
//            else
//            {
//                $player->setTurn(true);
//            }
//        }
//    }
//
// /* Displays which player's turn it is.
//  */foreach ($players0 as $player)
//    {
//        if ($player->isTurn())
//        {
//            $HTML .= "\nIt's now ".$player->getName()."'s turn!";
//            /*
//            $HTML->newLine();
//            $HTML->addText("It's now ".$player->getName()."'s turn!");
//            $HTML->newLine();*/
//        }
//    }
//    
//    return $HTML;
//}

/**********************************FUNCTION********************************** *
* Function Name: buildPage
* Author: Nicholas Malacarne
* Parameters: $players - An array of PlayerDef objects.
*             $HTML - Our HTMLMgrDef obj.
* Description: Builds the Battle game page.
***************************************************************************** */
function buildPage($players, $HTML)
{
 /* Starts the HTML <head> section of our page.
  */$HTML->startHTMLHead();
 
    /*TODO: Add JavaScript here! */
 
    $HTML->endHTMLHead();
    
 /* Starts the HTML <body> section of our page.
  */$HTML->startHTMLBody();
  
 /* Adds the Glyph logo to the body and centers it.
  */$HTML->addTag("BODY", "center", $HTML->getImg(MAIN_LOGO));
	print "adding logo<br>";
    
 /* Builds and centers the Report Box, Status Box, and Hand.
  */$HTML->addTag("BODY", "center", buildReportBox($HTML));
	print "adding report box<br>";
    $HTML->addTag("BODY", "center", buildStatusBox($players, $HTML));
	print "adding status box<br>";
    $HTML->addTag("BODY", "center", buildHand($players, $HTML));
	print "adding hand<br>";
    
    $HTML->endHTMLBody();
    
 /* Adds our entities to the actual page.
  */$HTML->glue($HTML->getEntity("HEAD"));
    $HTML->glue($HTML->getEntity("BODY"));

/*TODO: Add HTMLMgrDef functionality for ink/combine buttons.*/
// /* Creates button/form for creating Ink.
//  */$form = $HTML->gAddText("<input type=\"image\" 
//                            src = \"".CREATE_INK_BTN."\">\n");
//    $form .= $HTML->gTextBox("hidden",
//                             "page",
//                             "ink");
//
//    $HTML->form('post', 'battle.php', $form);
//
////     /* If the player has Ink, print the button/form for combining.
////      */foreach ($players1 as $player)
////        {
////            if ($player->isTurn())
////            {
////                if ($player->getInk() > 0)
////                {
////                    $form = $HTML->gaddText("<input type=\"image\"
////                                            src = \"http://cfentertainment.freevar.com/images/Glyph/combine.jpg\">\n");
////                    $HTML->form('post', 'combine.php', $form);
////                }
////            }
////        }
//
//    $HTML->addHTML("</center>");
//
//    $HTML->buildBottom();
//
//    return $HTML->getPage();
 
}

/**********************************FUNCTION********************************** *
* Function Name: buildReportBox
* Author: Nicholas Malacarne
* Parameters: $HTML - Our HTMLMgrDef obj.
* Description: Builds the report box area of the page.
***************************************************************************** */
function buildReportBox($HTML)
{
    $HTML->createEntity("reportBox");

    $HTML->addTextArea("reportBox", 40, 10, $HTML->getEntity("gameReport"), 
                       true);
    
    return $HTML->getEntity("reportBox");
}

/**********************************FUNCTION********************************** *
* Function Name: buildStatusBox
* Author: Nicholas Malacarne
* Parameters: $players - An array of PlayerDef objects.
*             $HTML - Our HTMLMgrDef obj.
* Description: Builds the status box area of the page.
***************************************************************************** */
function buildStatusBox($players, $HTML)
{
 /* Creates the entities we need for this section of the page.
  * @statusBox - A table to hold two playerStats tables.
  * @playerStats - Holds the player name, blood, ink, and status effects.
  */$HTML->createEntity("statusBox");
    $HTML->createEntity("playerStats");

 /* Starts the statusBox table and adds a header.
  */$HTML->startTable("statusBox", 1);
    $HTML->startTRow("statusBox");
    $HTML->addTHeader("statusBox", "Stats:", 2);
    $HTML->endTRow("statusBox");
    $HTML->startTRow("statusBox");

 /* Cycles through the players and creates a table to hold each players' stats.
  */foreach ($players as $player)
    {    
     /* Starts the table and adds the players name as a header.
      */$HTML->startTable("playerStats", 1);
        $HTML->startTRow("playerStats");
        $HTML->addTHeader("playerStats", $player->getName());
        $HTML->endTRow("playerStats");
        $HTML->startTRow("playerStats");

     /* Grabs the player's Blood and Ink and formats them.
      */$blood = $HTML->getTag("b", "Blood: ").$player->getBlood()."\n";
        $ink   = $HTML->getTag("b", "Ink: ").$player->getInk();

     /* Inserts blood and ink into a data section of the row.
      */$HTML->addTData("playerStats", $blood.$ink);
      
        $HTML->endTRow("playerStats");
        
     /* Start a row and add in the status effects the player has.
      */$HTML->startTRow("playerStats");
        $HTML->addTData("playerStats", $player->getStatusReport()); 
        $HTML->endTRow("playerStats");
        
        $HTML->endTable("playerStats");
        
     /* Insert the playerStats table into the statusBox table.
      */$HTML->addTData("statusBox", $HTML->getEntity("playerStats"));  
    }
    
    $HTML->endTRow("statusBox");
    $HTML->endTable("statusBox");
    
    return $HTML->getEntity("statusBox");
}

/**********************************FUNCTION********************************** *
* Function Name: buildHand
* Author: Nicholas Malacarne
* Parameters: $players - An array of PlayerDef objects.
* Description: Displays what cards currently reside in the player's hand.
***************************************************************************** */
function buildHand($players, $HTML)
{
    $HTML->createEntity("hand");
    
 /* Creates a table to hold the player's hand.
  */$HTML->startTable("hand", 1);
    $HTML->startTRow("hand");
    $HTML->addTHeader("hand", "Hand:", 6);
    $HTML->endTRow("hand");
    $HTML->startTRow("hand");

    foreach ($players as $player)
    {
     /* Which player's turn is it?
      */if ($player->isTurn())
        {
         /* Fetches the player's hand.
          */$cards = $player->getHand();
			print "getting ".$player->getName()."'s hand.<br>";

            foreach ($cards as $key => $card)
            {
				print "adding card: ".$key." @\"".$card->getImage."\"<br>";
                $HTML->createEntity("card");
                
             /* Adds the image, sets up a form, and adds a image submit button.
              */$HTML->addImg("card", $card->getImage());
                $HTML->startForm("card", "cardForm", "battle.php");
                $HTML->addInBox("card", "hidden", "playedCard", $key);
                $HTML->addInBox("card", "hidden", "action", "invoke");
                $HTML->addTag("card", "center", $HTML->getInImg(INVOKE_BTN));
                $HTML->endForm("card");
                $HTML->addTData("hand", $HTML->getEntity("card"));
            }
        }
    }
    
    $HTML->endTRow("hand");
    $HTML->endTable("hand");
        
    return $HTML->getEntity("hand");
}

/*TODO: Move to GameMgrDef!*/
/**********************************FUNCTION********************************** *
* Function Name: addInk
* Author: Nicholas Malacarne
* Parameters: $players7 - An array of PlayerDef objects.
* Description: Gives the player an Ink point.
***************************************************************************** */
//function addInk($players6, $maxBlood)
//{
//    $HTML = new SuperHTMLDef();
//
//    foreach ($players6 as $player)
//    {
//        if ($player->isTurn())
//        {
//            $player->setBlood(-5);
//            $HTML->addText($player->getName()." loses 5 Blood!");
//            $HTML->newLine();
//            $player->setInk(1);
//            $HTML->addText($player->getName()." gains 1 Ink!");
//            $HTML->newLine();
//        }
//    }
//
//    return $HTML->getPage();
//}

/*TODO: Move to GameMgrDef!*/
/**********************************FUNCTION********************************** *
* Function Name: applyEffects
* Author: Nicholas Malacarne
* Parameters: $players7 - An array of PlayerDef objects.
* Description: Assigns the effects of each of the player's status'.
***************************************************************************** */
//function applyEffects($players7)
//{
//    $HTML = new SuperHTMLDef();
//
//    foreach ($players7 as $player)
//    {
//        if ($player->isTurn())
//        {
//            $report = $player->assignEffects();
//            $player->decayStatus();
//
//            if ($report == null)
//            {
//                break;
//            }
//            else
//            {
//                $HTML->addText($report);   
//            }    
//        }
//    }
//
//    return $HTML->getPage();
//}
?>
</html>
